package com.ahuan.controller;

import com.ahuan.domain.Module;
import com.ahuan.domain.ModuleNode;
import com.ahuan.exception.BusinessException;
import com.ahuan.resp.Res;
import com.ahuan.service.ModuleService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/Module")
public class ModuleController {


    @Autowired
    private ModuleService moduleService;
    // 模块管理
    @PostMapping("/list")
    public Res getModuleList() {
        try {
            List<Module> moduleTree = moduleService.getModuleTree();
            if(moduleTree != null&& moduleTree.size() > 0){
                return Res.successOperate(moduleTree);
            }
            return Res.failOperate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("菜单树查询失败");
        }
    }

//     模块管理
    @PostMapping("/nodes")
    public Res getModuleNodes() {
        try {
            List<Module> moduleTree = moduleService.getModuleList();
            if(moduleTree != null&& moduleTree.size() > 0){
                return Res.successOperate(moduleTree);
            }
            return Res.failOperate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("菜单列表询失败");
        }
    }

//     模块管理
    @GetMapping("/get/{id}")
    public Res getModuleMsg(@PathVariable String id) {
        try {
            log.info("id:{}",id);
            ModuleNode moduleNode = moduleService.queryModuleMsg(id);
            if(moduleNode != null){
                return Res.successOperate(moduleNode);
            }
            return Res.failOperate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("菜单项查询失败");
        }
    }

    @PostMapping("/save")
    public Res saveModuleNode(ModuleNode node) {
        try {
            String userId = MDC.get("userId");
            node.setAddUser(userId);
            log.info("saveModuleNode:{},UserId:{}",node,userId);
            if(!"".equals(node.getModuleId())){
                if(moduleService.editNode(node)){
                    return  Res.successOperate();
                }
                return Res.failOperate();
            }else {

                if(moduleService.addNode(node)){
                    return  Res.successOperate();
                }
                return Res.failOperate();
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            throw new BusinessException("菜单项保存失败");
        }

//        return Res.failOperate();
    }


    @DeleteMapping("/delete")
    public Res deleteModuleNode(@RequestParam String ids) {
        try {
            log.info("deleteModuleNode:{}",ids);
            if(moduleService.deleteModuleNode(ids)){
                return  Res.successOperate();
            }
            return  Res.failOperate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("菜单项删除失败");
        }
    }

}
